var merchantService = require('../service/merchant.service');
const _ = require('lodash');
const winston = require('winston');
var request = require('request');
var constant = require('../config/constant.config');

module.exports = {
    addMerchant:addMerchant,
    findMerchantByStatus:findMerchantByStatus,
    findMerchantByBan:findMerchantByBan,
    updateStatusById:updateStatusById,
    updateBanById:updateBanById,
    updateMerchent:updateMerchent,
    findAll:findAll,
    findMerchantById:findMerchantById,
    findStatusById:findStatusById
}

/**
 * find merchant status by id
 * @param req
 * @param res
 * @param next
 */
function findStatusById(req,res,next){
    winston.info('client find status by id');
    var id = req.params.id;
    if(_.isEmpty(id)){
        next(new Error('id param can not be empty'));
    }else {
        merchantService.findStatusById(id,function(err,doc){
            if(err){
                res.json({result:false,errMsg:'find status error'})
            }else {
                if(!doc){
                    res.json({result:false,errMsg:'can not find the merchant of id '+id});
                }else {
                    res.json({result:true,status:doc.status,rejectContent:doc.rejectContent});
                }
            }
        });
    }
}

/**
 * find merchant by id
 * @param req
 * @param res
 * @param next
 */
function findMerchantById(req,res,next){
    winston.info('client findMerchantById');
    var id = req.params.id;
    if(_.isEmpty(id)){
        next(new Error('id param can not be empty'));
    }else {
        merchantService.findMerchantById(id,function(err,doc){
            if(err){
                res.json({result:false,errMsg:'find merchant fail'});
            }else {
                res.json({result:true,data:doc});
            }
        });
    }
}

/**
 * find all merchants
 * @param req
 * @param res
 * @param next
 */
function findAll(req,res,next){
    winston.info('client find all merchants');
    merchantService.findAll(function(err,docs){
        if(err){
            res.json({result:false,errMsg:'find all merchant fail'});
        }else {
            res.json({result:true,data:docs});
        }
    });
}

/**
 * update merchant data by id
 * @param req
 * @param res
 * @param next
 */
function updateMerchent(req,res,next){
    winston.info('client update merchant');
    var mer = req.body;
    if(_.isEmpty(mer)){
        next(new Error('update data can not be empty'));
    }else {
        merchantService.updateMerchent(mer,function(err,result){
            if(err){
                res.json({result:false,errMsg:'update merchant fail'});
            }else {
                res.json({result:true,data:result});
            }
        });
    }
}

/**
 * update merchant ban status by id
 * @param req
 * @param res
 * @param next
 */
function updateBanById(req,res,next){
    winston.info('client update ban by id');
    var body = req.body;
    if(_.isEmpty(body)){
        next(new Error('body can not be empty!'));
        return;
    }
    if(_.isEmpty(body.id)){
        next(new Error('id can not be null!'));
        return;
    }
    merchantService.updateBanById(body.ban,body.id,function(err,doc){
        if(err){
            res.json({result:false,errMsg:'update merchant ban fail'});
        }else {
            _pushBanStatus(body.ban,body.id);
            res.json({result:true,data:doc});
        }
    });
}

//push status
function _pushBanStatus(ban,mid){
    winston.info('push status to M');
    var url = constant.BAN_STATUS_PUSH_URL+mid+'/'+ban;
    request.get({url:url},function(err,res,body){
        if(err){
            winston.error('push status to M system fail');
        }else {
            console.log(body);
            var rs = JSON.parse(body);
            console.log(rs);
        }
    });
}

/**
 * update merchant status by id
 * @param req
 * @param res
 * @param next
 */
function updateStatusById(req,res,next){
    winston.info('client update status by id');
    var body = req.body;
    if(_.isEmpty(body)){
        next(new Error('body can not be empty!'));
        return;
    }
    if(_.isEmpty(body.id)){
        next(new Error('id can not be null!'));
        return;
    }
    merchantService.updateStatusById(body.status,body.id,function(err,doc){
        if(err){
            res.json({result:false,errMsg:'update merchant status fail'});
        }else {
            res.json({result:true,data:doc});
        }
    });
}

/**
 * find merchants by ban
 * @param req
 * @param res
 * @param next
 */
function findMerchantByBan(req,res,next){
    winston.info('client find Merchant By ban status');
    var ban = req.params.ban;
    if(_.isEmpty(ban)){
        next(new Error('ban param can not be empty!'))
    }else {
        merchantService.findMerchantByBan(ban,function(err,docs){
            if(err){
                res.json({result:false,errMsg:'find merchants fail'});
            }else {
                var ids = [];
                _.forEach(docs,function(item){
                    ids.push(item._id);
                });
                res.json({result:true,data:ids});
            }
        });
    }
}

/**
 * find merchants by status
 * @param req
 * @param res
 * @param next
 */
function findMerchantByStatus(req,res,next){
    winston.info('client find Merchant By status');
    var status = req.params.status;
    if(_.isEmpty(status)){
        next(new Error('status param can not be empty!'))
    }else {
        merchantService.findMerchantByStatus(status,function(err,docs){
            if(err){
                res.json({result:false,errMsg:'find merchants fail'});
            }else {
                res.json({result:true,data:docs});
            }
        });
    }
}

/**
 * add merchant
 * @param req
 * @param res
 * @param next
 */
function addMerchant(req,res,next){
    winston.info('client add merchant');
    var mer = req.body;
    if(_.isEmpty(mer)){
        next(new Error('merchant data can not be empty!'))
    }else {
        merchantService.addMerchant(mer,function (err,doc) {
            if(err){
                res.json({result:false,errMsg:'add merchant fail'});
            }else {
                res.json({result:true,data:doc});
            }
        });
    }
}